package com.ice.body.dashboard;

import com.ice.base.model.BaseModel;
import com.ice.base.model.SysLoginModel;
import com.ice.base.result.AjaxResult;
import com.ice.base.result.ResponseResult;
import com.ice.base.util.RedisUtil;
import com.ice.body.business.model.FaceLogModel;
import com.ice.body.business.model.RecordLab;
import com.ice.body.business.model.WarningModel;
import com.ice.body.business.service.ReagentStockService;
import com.ice.body.business.service.RecordLabService;
import com.ice.body.business.service.WarningService;
import com.ice.body.dashboard.model.TotalStockVo;
import com.ice.body.hardware.service.FaceLogService;
import com.ice.body.log.model.LogAir;
import com.ice.body.log.model.LogAirVo;
import com.ice.body.log.service.LogAirService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
public class DashBoardController extends BaseModel {

    @Resource
    private ReagentStockService reagentStockService;
    @Resource
    private RecordLabService recordLabService;
    @Resource
    private FaceLogService faceLogService;
    @Resource
    private LogAirService logAirService;
    @Resource
    private RedisUtil redisUtil;



    @Resource
    private WarningService warningService;



    @ApiOperation(value = "告警记录")
    @PostMapping("/dashboard/selectWarn")
    public AjaxResult<WarningModel> selectWarn() {
        AjaxResult r = ResponseResult.success();
        try{
            ArrayList<WarningModel> warningModels = warningService.selectAll();
            r.setData(warningModels);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();
        }
        return r;
    }


    @ApiIgnore(value = "记录-刷脸")
    @PostMapping("/dashboard/selectFaceLog")
    public AjaxResult<FaceLogModel> selectFaceLog() {

        AjaxResult r = ResponseResult.success();
        List list;
        try{
            list = faceLogService.searchFaceLogList(new FaceLogModel());
            r.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();
        }
        return r;
    }

    @ApiOperation(value = "查询当前试剂库中用户")
    @PostMapping("/dashboard/onlineUser")
    public AjaxResult<SysLoginModel> loadUserSelect(HttpServletRequest request){
        AjaxResult r = ResponseResult.success();
        List<SysLoginModel> selectList = (List<SysLoginModel>)redisUtil.get(1+"userList");
        r.setData(selectList);
        return r;
    }


    //试剂总数
    @PostMapping("/dashboard/getTotalStock")
    public AjaxResult getTotalStock(){
        AjaxResult r = AjaxResult.success();
        ArrayList<TotalStockVo> list =  null;
        try{
            list =  reagentStockService.getTotalStock();
            r.setData(list);
            return r;
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    //出入归还记录
    @PostMapping("/dashboard/getRecord")
    public AjaxResult getRecord(){
        AjaxResult r = AjaxResult.success();
        List<RecordLab> list =  null;
        try{
            list =  recordLabService.selectAll();
            r.setData(list);
            return r;
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    /**
     * 空气质量
     * @return
     */
    @PostMapping("/dashboard/getTvoc")
    public AjaxResult getTvoc(){
        AjaxResult r = AjaxResult.success();
        try{
            LogAir logAir = logAirService.selectLast();
            ArrayList<LogAirVo> logAirs = new ArrayList<>();
            LogAirVo v1  = new LogAirVo();
            LogAirVo v2  = new LogAirVo();
            LogAirVo v3  = new LogAirVo();
            LogAirVo v4  = new LogAirVo();
            LogAirVo v5  = new LogAirVo();
            LogAirVo v6  = new LogAirVo();
            LogAirVo v7  = new LogAirVo();
            //甲醛 formaldehyde
            v1.setName("甲醛");
            v1.setValue(logAir.getFormaldehyde().toString());
            logAirs.add(v1);
            v2.setName("tvoc");
            v2.setValue(logAir.getTvoc().toString());
            logAirs.add(v2);
            v3.setName("pm2.5");
            v3.setValue(logAir.getPm().toString());
            logAirs.add(v3);
            v4.setName("温度");//temperature
            v4.setValue(logAir.getTemperature().toString());
            logAirs.add(v4);
            v5.setName("湿度");//humidity
            v5.setValue(logAir.getHumidity().toString());
            logAirs.add(v5);
            v6.setName("空气质量");//airQuality
            v6.setValue(logAir.getAirQuality().toString());
            logAirs.add(v6);
            //carbonDioxide
            v7.setName("二氧化碳");
            v7.setValue(logAir.getCarbonDioxide().toString());
            logAirs.add(v7);

          //  ArrayList<LogAir> logAirs = logAirService.selectLimit();
            r.setData(logAirs);
            return r;
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }



/*
    // "统一设备报警表-查询多个")
    @PostMapping("/dashBoard/selectAllWarn")
    public AjaxResult<WarningDeviceModel> selectAllWarn(WarningDeviceModel model) {
        AjaxResult r = AjaxResult.success();
        try{
            List<WarningDeviceModel> list = warningDeviceService.selectListByModel(model);
            r.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            r = AjaxResult.error();
        }
        return r;
    }

*/

}
